package com.example.lj.uav.adapter;

import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import com.example.lj.uav.R;
import com.example.lj.uav.bean.PointAction;
import com.example.lj.uav.holder.ActionHolder;

import java.util.ArrayList;
import java.util.List;

public class ActionAdapter extends RecyclerView.Adapter<ActionHolder>{

    private List<PointAction> datalist = new ArrayList<>();
    private Handler handler;
    private String[] actionStrs = {"悬停","拍照","开始录像","停止录像","飞行器航偏角","云台俯仰角"};
    private String[] actionIds = {"Hovering","ShootPhoto","StartRecording","StopRecording","AircraftYaw","GimbalPitch"};

    public ActionAdapter(List<PointAction> datalist,Handler handler) {
        this.datalist = datalist;
        this.handler = handler;
    }

    @Override
    public ActionHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_point_action, parent, false);
        return new ActionHolder(view);
    }

    @Override
    public void onBindViewHolder(final ActionHolder holder, final int position) {
        final PointAction action = datalist.get(position);
        String id = datalist.get(position).getName();
        String name = getNameById(id);
        holder.mTvName.setText(name);
        if (action.getName().equals("Hovering")){
            int param = Integer.parseInt(datalist.get(position).getParam());
            int data = param/1000;
            holder.mTvParamSec.setText(data+"s");
            holder.mLlSecond.setVisibility(View.VISIBLE);
            holder.mLlAngle.setVisibility(View.GONE);
            holder.mIvReduceSec.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int param = Integer.parseInt(datalist.get(position).getParam());
                    if (param==0){
                        return;
                    }
                    param = param-1000;
                    holder.mTvParamSec.setText(param/1000+"s");
                    datalist.get(position).setParam(param+"");
                }
            });
            holder.mIvPlusSec.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int param = Integer.parseInt(datalist.get(position).getParam());
                    if (param>=30000){
                        return;
                    }
                    param = param+1000;
                    holder.mTvParamSec.setText(param/1000+"s");
                    datalist.get(position).setParam(param+"");
                }
            });
            holder.mTvParamSec.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    holder.mTvParamSec.setVisibility(View.GONE);
                    holder.mEtSecond.setVisibility(View.VISIBLE);
                    holder.mEtSecond.setText(datalist.get(position).getParam());
                }
            });
            holder.mEtSecond.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                    if (actionId== EditorInfo.IME_ACTION_DONE||actionId== EditorInfo.IME_ACTION_NEXT||
                            actionId== EditorInfo.IME_ACTION_UNSPECIFIED||actionId== EditorInfo.IME_ACTION_SEND){
                        String content = holder.mEtSecond.getText().toString().trim();
                        int qty = Integer.parseInt(content);
                        if (qty<=0){
                            qty = 0;
                        }
                        if (qty>=30){
                            qty = 30;
                        }
                        holder.mTvParamSec.setVisibility(View.VISIBLE);
                        holder.mTvParamSec.setText(qty+"s");
                        datalist.get(position).setParam(qty+"");
                        holder.mEtSecond.setVisibility(View.GONE);
                    }
                    return false;
                }
            });
        }else if (action.getName().equals("AircraftYaw")||action.getName().equals("GimbalPitch")){
            holder.mLlSecond.setVisibility(View.GONE);
            holder.mLlAngle.setVisibility(View.VISIBLE);
            holder.mTvParamAngle.setText(datalist.get(position).getParam()+"°");
            holder.mIvReduceAngle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int param = Integer.parseInt(datalist.get(position).getParam());
                    --param;
                    if (param<=-180){
                        param = -180;
                    }
                    holder.mTvParamAngle.setText(param+"°");
                    datalist.get(position).setParam(param+"");
                }
            });
            holder.mIvPlusAngle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int param = Integer.parseInt(datalist.get(position).getParam());
                    if (param>=0&&action.getName().equals("GimbalPitch")){
                        return;
                    }
                    ++param;
                    if (param>=180){
                        param = 180;
                    }
                    holder.mTvParamAngle.setText(param+"°");
                    datalist.get(position).setParam(param+"");
                }
            });
            holder.mTvParamAngle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    holder.mTvParamAngle.setVisibility(View.GONE);
                    holder.mEtAngle.setVisibility(View.VISIBLE);
                    holder.mEtAngle.setText(datalist.get(position).getParam());
                }
            });
            holder.mEtAngle.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                    if (actionId== EditorInfo.IME_ACTION_DONE||actionId== EditorInfo.IME_ACTION_NEXT||
                            actionId== EditorInfo.IME_ACTION_UNSPECIFIED||actionId== EditorInfo.IME_ACTION_SEND){
                        String content = holder.mEtAngle.getText().toString().trim();
                        int qty = Integer.parseInt(content);
                        if (qty<=-180){
                            qty = -180;
                        }
                        if (action.getName().equals("GimbalPitch")){
                            if (qty>=0){
                                qty = 0;
                            }
                        }else{
                            if (qty>=180){
                                qty = 180;
                            }
                        }
                        holder.mTvParamAngle.setVisibility(View.VISIBLE);
                        holder.mTvParamAngle.setText(qty+"°");
                        datalist.get(position).setParam(qty+"");
                        holder.mEtAngle.setVisibility(View.GONE);
                    }
                    return false;
                }
            });
        }else{
            holder.mLlSecond.setVisibility(View.GONE);
            holder.mLlAngle.setVisibility(View.GONE);
        }
//        holder.mRlDelete.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                datalist.remove(position);
//                notifyDataSetChanged();
//            }
//        });
        holder.mIvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                datalist.remove(position);
                notifyDataSetChanged();
            }
        });
    }

    private String getNameById(String id) {
        for (int i=0;i<actionIds.length;i++){
            if (actionIds[i].equals(id)){
                return actionStrs[i];
            }
        }
        return "";
    }

    @Override
    public int getItemCount() {
        return datalist.size();
    }

    public List<PointAction> getDatalist(){
        return datalist;
    }

    public void fillList(List<PointAction> list){
        datalist.clear();
        datalist.addAll(list);
        notifyDataSetChanged();
    }
}
